//
//  doorsTableViewController.m
//  HaiKangDemo
//
//  Created by 崔 on 2016/11/15.
//  Copyright © 2016年 崔. All rights reserved.
//

#import "doorsTableViewController.h"
#import "LGController.h"
#import "doorTableViewCell.h"
#import "relevanceInfo.h"
#import "LGControllerManager.h"
#define CHYRELEVANCEINFONOTS @"CHYrelevanceInfoNots"

static NSString *ID = @"doorID";

@interface doorsTableViewController ()

@property (nonatomic , weak)UIImageView *tempImageView;

@property (nonatomic , weak)UIButton *tempBtn;
//关联信息
@property (nonatomic,strong)relevanceInfo *relevanceInfo;

@property (nonatomic ,strong)NSMutableDictionary *dict ;


@end

@implementation doorsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置导航条上的内容
//    [self setNav];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([doorTableViewCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
    //设置tableView 背景
    self.tableView.backgroundColor = [UIColor clearColor];
//    UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroung"]];
    
//    [self.tableView setBackgroundView:backImage];
    
   
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNav];
}

- (NSMutableDictionary *)dict{
    
    if (_dict == nil) {
        
        _dict = [NSMutableDictionary dictionary];
    }
    
    return _dict;
}


- (void)setNav {
    
    if (self.parentViewController) {
        UIViewController *vc = self.parentViewController;
        
        vc.navigationItem.title = @"关联门";

        
        UIButton *leftBtn = [[UIButton alloc] init];
        
        [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [leftBtn setTitleColor:[UIColor colorWithRed:63/255.0 green:235/255.0 blue:235/255.0 alpha:1] forState:UIControlStateHighlighted];
        
        [leftBtn sizeToFit];
        
        [leftBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        
        vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        
        UIButton *rightBtn = [[UIButton alloc] init];
        
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [rightBtn setTitleColor:[UIColor colorWithRed:63/255.0 green:235/255.0 blue:235/255.0 alpha:1] forState:UIControlStateHighlighted];
        
        [rightBtn sizeToFit];
        
        [rightBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        
        vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        
        //    self.navigationItem.rightBarButtonItem.enabled = NO;
        
        vc.navigationItem.rightBarButtonItem.accessibilityElementsHidden = YES;
    }
    return;
    
    UIButton *leftBtn = [[UIButton alloc] init];
    
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [leftBtn setTitleColor:[UIColor colorWithRed:63/255.0 green:235/255.0 blue:235/255.0 alpha:1] forState:UIControlStateHighlighted];
    
    [leftBtn sizeToFit];
    
    [leftBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton *rightBtn = [[UIButton alloc] init];
    
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [rightBtn setTitleColor:[UIColor colorWithRed:63/255.0 green:235/255.0 blue:235/255.0 alpha:1] forState:UIControlStateHighlighted];
    
    [rightBtn sizeToFit];
    
    [rightBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    //    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    self.navigationItem.rightBarButtonItem.accessibilityElementsHidden = YES;
}
#pragma mark - 内容控制方法
- (void)cancelClick
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
//保存
- (void)saveClick{
    
//    //发送一个通知
    [[NSNotificationCenter defaultCenter] postNotificationName:CHYRELEVANCEINFONOTS object:self.relevanceInfo];

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.controllers.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    LGController *controller = self.controllers[section];
    
    NSArray *doorM = controller.doors;
    
    NSMutableArray *exactDoors= [NSMutableArray array];
    
    for (LGDoor *door  in doorM) {
        
        //如果门有名称并且能显示,过滤掉没名字,且不需要显示的门信息
        if (door.show && door.valid) {
            
            [exactDoors addObject:door];
        }
        
    }
    
    return exactDoors.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    doorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    LGController *controller = self.controllers[indexPath.section];
    
     NSArray *doorM = controller.doors;
    
    NSMutableArray *exactDoors= [NSMutableArray array];
    
    for (LGDoor *door  in doorM) {
        
        //如果门有名称并且能显示,过滤掉没名字,且不需要显示的门信息
        if (door.show && door.valid) {
            
            [exactDoors addObject:door];
            
        }
        
    }
    LGDoor *door = exactDoors[indexPath.row];
    
    cell.name = door.doorName;
    
    return cell;
}


//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    doorTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    if ([self.tempBtn isEqual:cell.selectMarkBtn]) {
        
        self.tempBtn.selected = NO;
        
        self.tempBtn = nil;
        
        self.relevanceInfo = nil;

        return;
    }
    
    self.tempBtn.selected = NO;
    
    self.tempBtn = cell.selectMarkBtn;
        
    self.tempBtn.selected = YES;
    
    
    LGController *controller = self.controllers[indexPath.section];
    
    self.relevanceInfo = [relevanceInfo relevanceInfoWithContrller:controller indexPath:indexPath];
    
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 53;
}

//设置头部视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 23;
}

//设置头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 23)];
    
    UILabel *lable = [[UILabel alloc] init];
    
    lable.frame = CGRectMake(16, 0, customView.frame.size.width, customView.frame.size.height);
    
    LGController *controller = self.controllers[section];
    
    lable.text = controller.name;
    
    //设置头部视图的名称
    lable.textColor = [UIColor colorWithRed:167/255.0 green:169/255.0 blue:168/255.0 alpha:1];
    
    lable.font = [UIFont systemFontOfSize:12.0];
    
    [customView addSubview:lable];
    
    return customView;

}


@end
